//
//  PYCHZWithMultiPYs.m
//  PinyinContacts
//
//  Created by Xiaoping Zhang on 2/20/10.
//  Copyright 2010 Filters, Inc. All rights reserved.
//

#import "PYCHZWithMultiPYs.h"
#import "PYCHzpy.h"

@implementation PYCHZWithMultiPYs

- (id)initWithHanzi:(NSString *)baseHanzi
{
	self = [super init];
	
	if (self)
	{
		pinyinArray = [[NSMutableArray array] retain];
		hanzi = [[NSString stringWithString:baseHanzi] retain];
		primaryPYIndex = NSNotFound;
	}
	
	return self;
}

- (void)dealloc
{
	[super dealloc];
	
	[hanzi release];
	[pinyinArray release];
	primaryPYIndex = NSNotFound;
}

- (NSString *)hanzi
{
	return hanzi;
}

- (void)setHanzi:(NSString *)newHanzi
{
	if (hanzi == newHanzi)
		return;
	
	[hanzi release];
	
	for(id hzpy in pinyinArray)
	{
		if ([hzpy respondsToSelector:@selector(setHanzi:)])
		{
			[hzpy setHanzi:newHanzi];
		}
	}
	
	hanzi = [[NSString stringWithString:newHanzi] retain];
}

- (NSUInteger) primaryPYIndex
{
	return primaryPYIndex;
}

- (void)setPrimaryPYIndex:(NSUInteger)newPrimaryPYIndex;
{
	if (primaryPYIndex == newPrimaryPYIndex || 
		pinyinArray == nil || [pinyinArray count] == 0 || 
		newPrimaryPYIndex == NSNotFound || newPrimaryPYIndex >= [pinyinArray count])
		return;
	
	PYCHzpy *primaryHzpy = [pinyinArray objectAtIndex:primaryPYIndex];
	PYCHzpy *newPrimaryHzpy = [pinyinArray objectAtIndex:newPrimaryPYIndex];
	
	NSInteger primaryPriority = [[primaryHzpy priority] intValue];
	NSInteger newPriority = [[newPrimaryHzpy priority] intValue];
	
	assert(primaryPriority > newPriority);

	[primaryHzpy setPriority:[NSNumber numberWithInt:newPriority]];
	[newPrimaryHzpy setPriority:[NSNumber numberWithInt:primaryPriority]];
	
	primaryPYIndex = newPrimaryPYIndex;
}

- (void)addPinyin:(PYCHzpy *)hzpy
{
	if (!hzpy)
		return;
	
	if (primaryPYIndex != NSNotFound)
	{
		PYCHzpy *primaryHzpy = [pinyinArray objectAtIndex:primaryPYIndex];
		if ([[primaryHzpy priority] intValue] < [[hzpy priority] intValue])
		{
			[pinyinArray addObject:hzpy];
			primaryPYIndex = [pinyinArray count] - 1;
		}
		else
		{
			[pinyinArray addObject:hzpy];
		}
	}
	else 
	{
		assert(pinyinArray == nil || [pinyinArray count] == 0);

		[pinyinArray addObject:hzpy];
		primaryPYIndex = [pinyinArray count] - 1;
	}
}

- (void)normalize
{
	if (primaryPYIndex == NSNotFound ||
		pinyinArray == nil || [pinyinArray count] <= 1 || primaryPYIndex >= [pinyinArray count])
	{
		assert(false);
		return;
	}
	
	PYCHzpy *primaryHzpy = [pinyinArray objectAtIndex:primaryPYIndex];
	int currentPrimaryPriority = [[primaryHzpy priority] intValue];
	int newPrimaryPriority = currentPrimaryPriority;
	
	for (PYCHzpy *hzpy in pinyinArray)
	{
		if (primaryHzpy == hzpy)
			continue;
		
		int priority = [[hzpy priority] intValue];
		if (newPrimaryPriority < priority)
		{
			newPrimaryPriority = priority;
		}
		else if (newPrimaryPriority == priority)
		{
			newPrimaryPriority = priority + 1;
		}
	}
	
	if (newPrimaryPriority > currentPrimaryPriority)
	{
		[primaryHzpy setPriority:[NSNumber numberWithInt:newPrimaryPriority]];
	}
}

- (NSArray *)pinyinList
{
	if (pinyinArray == nil || [pinyinArray count] == 0)
		return nil;
	
	NSMutableArray *resultArray = [NSMutableArray array];
	for (PYCHzpy *hzpy in pinyinArray)
	{
		[resultArray addObject:[hzpy pinyin]];
	}
	
	return resultArray;
}

- (void)deleteFromContext:(NSManagedObjectContext *)dbContext
{
	if (!dbContext)
		return;
	
	for (PYCHzpy *hzpy in pinyinArray)
	{
		// deleteObject will add reference count for hzpy, however, it seems 
		// hzpy will not be released any more.
		// TODO: check how to release hzpy;
		[dbContext deleteObject:hzpy];
	}
	
	// check wheter hzpy before will be released
	[pinyinArray release];
}

@end
